package com.CityHunter;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

public class FriendList {

	String result;

	List<Friend> Fl;
	Friend fd;
	int count;
	int length;
	SharedPreferences sp;

	public FriendList() {
		Fl = new ArrayList<Friend>();
		count = 0;
		
	}

	public void retriveFriendListByGroup(String groupTitle,String name) {
		Fl.clear();
		try {
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("grouptitle", groupTitle));
			nameValuePairs.add(new BasicNameValuePair("name", name));
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
			"http://tamama.slyrc.org/FYP/FYP/getGROUP_MEMBER.php");
	httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity);
			JSONArray json = new JSONArray(result);

			for (int i = 0; i < json.length(); i++) {
				Fl.add(new Friend(json.getJSONObject(i).getString("name"),
						Double.valueOf(json.getJSONObject(i).getString(
								"lastLongitude")), Double
								.valueOf(json.getJSONObject(i).getString(
										"lastLatitude"))));
				
				

			}
			length=json.length();

		} catch (Exception e) {
			Fl.add(new Friend("Wrong", 114.108589, 22.339865));
		}
	

	}
	
	public List<Friend> getFriendList(){
		return Fl;
	}

	public boolean hasNext() {
		if (count >= Fl.size()) {

			count = 0;
			return false;

		}

		return true;
	}

	public Friend Next() {
		return Fl.get(count++);

	}
	
	public int Length(){
		return length;
	}

}
